﻿function AzureRequest() {

    var self = this;

    var version = '2011-08-18';

    var query = {};

    this.uri = null;
    
    this.verb = 'GET';

    this.headers = {};

    this.downloadName = null;

    this.data = null;

    this.query = query;

    this.resource = '';

    this.signature = null;

    this.stringToSign = '';

    this.acceptBase64 = false;

    function getDate() {
        function formatInt(val) {
            return val < 10 ? '0' + val : val;
        }
        var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
        var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
        var dt = new Date();
        dt = new Date(dt.getTime() + dt.getTimezoneOffset() * 60 * 1000);
        var res = '';
        res += days[dt.getDay()];
        res += ', ';
        res += formatInt(dt.getDate());
        res += ' ';
        res += months[dt.getMonth()];
        res += ' ';
        res += dt.getFullYear();
        res += ' ';
        res += formatInt(dt.getHours());
        res += ':';
        res += formatInt(dt.getMinutes());
        res += ':';
        res += formatInt(dt.getSeconds());
        res += ' GMT';
        return res;
    }
    
    function refreshDate() {
        var date = getDate();
        if (self.headers['x-ms-date']) {
            self.headers['x-ms-date'] = date;
        }
        if (self.headers['Date']) {
            self.headers['Date'] = date;
        }
    }
    
    function sign(credentials) {
        var accountName = credentials.getAccountName();

        var uri = self.uri;
        if (!uri) return;
        
        var path = uri.getPathWithoutQuery();
        if (path.length == 0 || path.charAt(0) != '/') path = '/' + path;
        self.resource = '/' + accountName + encodeURI(path);

        var signature = new AzureSignature(credentials);
        var type = '';
        switch (self.signatureType) {
            case 'SharedKey':
                self.stringToSign = signature.getSharedKeySignature(self);
                type = 'SharedKey';
                break;
            case 'SharedKeyTables':
                self.stringToSign = signature.getSharedKeyTablesSignature(self);
                type = 'SharedKey';
                break;
            case 'SharedKeyLite':
                self.stringToSign = signature.getSharedKeyLiteSignature(self);
                type = 'SharedKeyLite';
                break;
            case 'SharedKeyLiteTables':
                self.stringToSign = signature.getSharedKeyLiteTablesSignature(self);
                type = 'SharedKeyLite';
                break;
        }
        self.headers.Authorization = type + ' ' + accountName + ':' + credentials.sign(self.stringToSign);
    }

    this.addQuery = function (name, value) {
        query[name] = value;
        this.uri.addQuery(name, value);
    };

    this.getQuery = function(name) {
        return query[name];
    };

    self.headers['x-ms-date'] = getDate();
    self.headers['x-ms-version'] = version;

    this.sign = sign;
    this.refreshDate = refreshDate;

}
